<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 前台父控制器
 */
class Home_Controller extends CI_Controller {
	public function __construct() {
		parent::__construct();
	}
}

/**
 * 后台父类控制器
 */
class Admin_Controller extends CI_Controller {
	public $data;
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('pagination');
		//判断是否已登录
		$flag = false;
		$auth = $this->session->userdata('admin');
		if(!empty($auth))
		{
			$this -> data['username'] = $auth->username;
			$flag = true;
		}
		if(!$flag)
		{
			redirect('admin/Login/index');
			exit(0);
		}
	}
}
/**
 * 接口父控制器
 */

class Base_Controller extends CI_Controller {
	public function __construct() {
		parent::__construct();
		// 控制器初始化
		$this->_initialize();
	}
	public function _initialize()
	{
		if(empty($this->input->post('access_token')))
		{
			$this->responseApi(false,'未定义参数');
//			$this->responseApi(true,'成功',['id'=>1,'name'=>'测试']);
			exit;
		}
		$access_token = $this->input->post('access_token');
		# 控制器名
		$controller=$this->router->fetch_class();
		# 方法名
		$action=$this->router->fetch_method();
		$date = date('Y-m-d',time());    //当前日期
		$key = 'jhtc123!@#456$%^';     //key串
		$md5 = md5($controller.$action.$date.$key);
		if ($md5 == $access_token)
		{
			$this->responseApi(true,'验证成功');
		}
		else
		{
			$this->responseApi(false,'验证失败');
			exit;
		}
	}
	/**
	 * 结果监控
	 * @param $rs
	 * @param string $msg
	 * @param int $code
	 * @param array $data
	 * @return Json
	 */
	protected function responseApi($rs, $msg = '', $data = [],  $code = 50000)
	{
		if (is_bool($rs))
		{
			return $rs ? $this->json_success($msg, $data) : $this->json_error($msg,$code, $data);
		}
		echo json_encode([
			'code' => 200,
			'data' => $rs
		]);
	}
	public function json_error($message = 'error', $code = 500, $data = [])
	{
		echo json_encode([
			'code' => $code,
			'msg' => $message,
			'data' => $data
		]);
	}
	public function json_success($message = 'success', $data = [])
	{
		echo json_encode([
			'code' => 200,
			'msg' => $message,
			'data' => $data
		]);
	}
	//加密函数
	public	function passport_encrypt($txt, $key) {
		srand((double)microtime() * 1000000);
		$encrypt_key = md5(rand(0, 32000));
		$ctr = 0;
		$tmp = '';
		for($i = 0;$i < strlen($txt); $i++) {
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
		}
		return base64_encode($this->passport_key($tmp, $key));
	}
	//解密函数
	public function passport_decrypt($txt, $key) {
		$txt = $this->passport_key(base64_decode($txt), $key);
		$tmp = '';
		for($i = 0;$i < strlen($txt); $i++) {
			$md5 = $txt[$i];
			$tmp .= $txt[++$i] ^ $md5;
		}
		return $tmp;
	}
	public function passport_key($txt, $encrypt_key) {
		$encrypt_key = md5($encrypt_key);
		$ctr = 0;
		$tmp = '';
		for($i = 0; $i < strlen($txt); $i++) {
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
		}
		return $tmp;
	}
}
